unit formSubSetSum4Makc;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  System.Generics.Collections,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Data.DB,
  Vcl.Menus,
  Vcl.ComCtrls,
  dxmdaset,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter,
  subSetSumMakcSolver;

type
  TfmSubSetSum4Makc = class(TForm)
    mdViewData: TdxMemData;
    dlgOpenFile: TOpenDialog;
    tcMain: TTabControl;
    gridDBTableViewMain: TcxGridDBTableView;
    gridLevelMain: TcxGridLevel;
    gridMain: TcxGrid;
    dsMain: TDataSource;
    gridDBTableViewMainRecId: TcxGridDBColumn;
    gridDBTableViewMainUserCode: TcxGridDBColumn;
    gridDBTableViewMainUserData: TcxGridDBColumn;
    mdViewDataUserCode: TWideStringField;
    mdViewDataUserData: TIntegerField;
    procedure tcMainChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    fViewDataList: TListOfCDList;
  private
    procedure makeTabs;
    procedure showData(const ADataIndx: integer);
  private
    procedure loadFile(const AFileName: string);
    procedure calcSubs;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  fmSubSetSum4Makc: TfmSubSetSum4Makc;

implementation

{$R *.dfm}

procedure TfmSubSetSum4Makc.afterConstruction;
begin
  inherited;
  //
  fViewDataList := TListOfCDList.Create;
end;

procedure TfmSubSetSum4Makc.beforeDestruction;
begin
  inherited;
  //
  fViewDataList.free;
end;

procedure TfmSubSetSum4Makc.calcSubs;
var solutionList: TListOfCDList;
begin
  solutionList := TListOfCDList.Create;
  try
    TSubSetSumMakcSolver.solveSubSetSum2(fViewDataList[0], 15, 0, solutionList);
    //
    fViewDataList.AddRange(solutionList);
  finally
    solutionList.Free;
  end;
end;

procedure TfmSubSetSum4Makc.FormShow(Sender: TObject);
begin
  if not dlgOpenFile.Execute then
    exit;
  //
  loadFile(dlgOpenFile.FileName);
  //
  calcSubs;
  //
  makeTabs;
  //
  showData(0);
end;

procedure TfmSubSetSum4Makc.loadFile(const AFileName: string);
var i           : integer;
    fileDiskData: TStringList;
    fileViewData: TCodeDataList;
    fileUserCode: TUserCodeType;
    fileUserData: TUserDataType;
begin
  if fViewDataList.Count = 0 then begin
    fileViewData := TCodeDataList.Create;
    fViewDataList.Add(fileViewData);
  end;
  //
  fileViewData := fViewDataList[0];
  //
  fileViewData.Clear;
  //
  fileDiskData := TStringList.Create;
  try
    fileDiskData.LoadFromFile(AFileName);
    //
    for i := 0 to fileDiskData.Count - 1 do begin
      fileUserCode := fileDiskData.Names[i];
      fileUserData := StrToInt(fileDiskData.Values[fileDiskData.Names[i]]);
      fileViewData.Add(fileUserCode, fileUserData);
    end;
  finally
    fileDiskData.free;
  end;
end;

procedure TfmSubSetSum4Makc.makeTabs;
var i, rawValue, sumValue: integer;
begin
  tcMain.Tabs.BeginUpdate;
  try
    if fViewDataList.Count = 0 then
      exit;
    //
    for i := 1 to fViewDataList.Count - 1 do begin
      sumValue := 0;
      for rawValue in fViewDataList[i].Values do
        sumValue := sumValue + rawValue;
      tcMain.Tabs.Add(IntToStr(sumValue));
    end;
  finally
    tcMain.Tabs.EndUpdate;
  end;
end;

procedure TfmSubSetSum4Makc.showData(const ADataIndx: integer);
var currentValue: TCodeDataItem;
    someViewData: TCodeDataList;
begin
  someViewData := fViewDataList[ADataIndx];
  //
  mdViewData.DisableControls;
  try
    while not mdViewData.isEmpty do
      mdViewData.delete;
    //
    for currentValue in someViewData do begin
      mdViewData.Append;
      try
        mdViewDataUserCode.Value := currentValue.Key;
        mdViewDataUserData.Value := currentValue.Value;
        ///
        mdViewData.Post;
      except
        mdViewData.Cancel;
        raise;
      end;
    end;
  finally
    mdViewData.EnableControls;
  end;
end;

procedure TfmSubSetSum4Makc.tcMainChange(Sender: TObject);
begin
  showData(tcMain.TabIndex);
end;

end.
